<template>
	<view class="container">
		<image src="../../static/logo.png" class="img-responsive img-circle" style="margin: auto;width: 200rpx;height: 200rpx;"></image>
		<view class="form-group">
			<view class="text-center">
				用户名
			</view>
			<view>
				<input type="text" v-model="userName" class="form-control" placeholder="请输入用户名" />
			</view>
		</view>
		<view class="form-group">
			<view class="text-center">
				密码
			</view>
			<view>
				<input type="password" v-model="password" class="form-control" placeholder="请输入密码" />
			</view>
		</view>
		<view>
			<button class="btn btn-block btn-primary" @click="login()" :disabled="isLogin">登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: '',
				password: '',
				isLogin: false
			}
		},
		mounted() {

		},
		methods: {
			login() {
				let obj = {
					userName: this.userName,
					password: this.password
				};
				this.isLogin = true;
				this.$request.ajaxPro({
					url: this.$api.loginUrl,
					data: obj,
					method: 'POST',
					isShowLoad: true
				}).then((res) => {
					if (res.status) {
						//给全局变量赋值
						getApp().globalData.user = res.data;
						getApp().globalData.userId = res.data.id;
						getApp().globalData.token = res.token;
						//把用户信息放在本地
						uni.setStorage({
							key: getApp().globalData.storageUserKey,
							data: res.data
						});
						uni.setStorage({
							key: getApp().globalData.storageTokenKey,
							data: res.token
						});
						uni.showModal({
							title: '登录成功',
							content: '跳转至主页',
							showCancel: false,
							success(res) {
								if (res.confirm) {
									uni.switchTab({
										url: '../index/index'
									});
								}
							}
						});
					} else {
						this.password = '';
						this.isLogin = false;
					}
				}, (err) => {
					this.isLogin = false;
					console.log(err)

				});
			}
		}
	}
</script>

<style>
	.container {
		margin-top: 150rpx;
	}
</style>
